﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Model;
using Ecreate.Business;

namespace Ecreate.Web.systemanage.product
{
    public partial class productcrud1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string actiontype = Request.QueryString["type"];
                if (actiontype == "edit")
                {
                    ProductService service = new ProductService();
                    Product model = new Product();
                    model.Pid = int.Parse(this.Request.QueryString["pid"]);
                    model = service.getProductByID(model);
                    hid_classid.Value = model.Classid;
                    txtPname.Text = model.Pname;
                    txtPdescription.Text = model.Pdescription;
                    txtPrice.Text = model.Price.ToString();
                    ckbIsHot.Checked = model.IsHot == "1" ? true : false;
                    ckbIsSale.Checked = model.IsSale == "1" ? true : false;
                    ckbIsDown.Checked = model.IsDown == "1" ? true : false;
                    imgPreview.ImageUrl = "~" + model.Pimageurl;
                }
                else if (actiontype == "delete")
                {
                    string Classid = Request.QueryString["classid"];
                    try
                    {
                        ProductService service = new ProductService();
                        Product model = new Product();
                        model.Pid = int.Parse(this.Request.QueryString["pid"]);
                        service.Delete(model);
                        string str_alert = "<script type=\"text/javascript\" language=\"javascript\">          alert('删除成功')　　; </script>";
                        string str_url = "<script type=\"text/javascript\" language=\"javascript\">          window.parent.frames[\"productDtlIframe\"].location=\"/systemanage/product/productlist.aspx?classid=" + Classid + "\"　　; </script>";
                        Response.Write(str_alert);
                        Response.Write(str_url);
                        return;
                    }
                    catch
                    {
                        string str_alert = "<script type=\"text/javascript\" language=\"javascript\">           alert('不存在该产品,请刷新显示数据!')　　; </script>";
                        string str_url = "<script type=\"text/javascript\" language=\"javascript\">          window.parent.frames[\"productDtlIframe\"].location=\"/systemanage/product/productlist.aspx?classid=" + Classid + "\"　　; </script>";
                        Response.Write(str_alert);
                        Response.Write(str_url);
                        return;
                    }
                }
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile) 
            {
                SaveFile(FileUpload1.PostedFile);
            }
            else
            {
            }
        }

        private void SaveFile(HttpPostedFile httpPostedFile)
        {
            string uploadpath = @"~/content/images/upload/";
            string savePath = Server.MapPath(@"~\content\images\upload\");
            string fileName = FileUpload1.FileName;
            //string pathToCheck = savePath + fileName;
            //string tempfileName = "";
            //if (System.IO.File.Exists(pathToCheck))
            //{
            //    int counter = 2;
            //    while (System.IO.File.Exists(pathToCheck))
            //    {
            //        tempfileName = counter.ToString() + fileName;
            //        pathToCheck = savePath + tempfileName;
            //        counter = counter + 1;
            //    }
            //    fileName = tempfileName;
            //}
            //else
            //{
            //}

            fileName = System.IO.Path.GetFileName(FileUpload1.FileName);
            string fileExtension = System.IO.Path.GetExtension(fileName);
            Random objRand = new Random();
            System.DateTime date = DateTime.Now;
            //生成随机文件名
            string saveName = date.Year.ToString() + date.Month.ToString() + date.Day.ToString() + date.Hour.ToString() + date.Minute.ToString()
                + date.Second.ToString() + Convert.ToString(objRand.Next(99) * 97 + 100);
            fileName = saveName + fileExtension;

            savePath += fileName;
            FileUpload1.SaveAs(savePath);

            imgPreview.ImageUrl = uploadpath + fileName;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ProductService service = new ProductService();
            Product model = new Product();
            model.Pdescription = txtPdescription.Text;
            model.Pname = txtPname.Text;
            model.Pimageurl = imgPreview.ImageUrl.Replace("~", "");
            decimal o;
            if (decimal.TryParse(txtPrice.Text, out o))
            {
                model.Price = Convert.ToDecimal(txtPrice.Text);
            }
            model.IsHot = ckbIsHot.Checked ? "1" : "0";
            model.IsSale = ckbIsSale.Checked ? "1" : "0";
            model.IsDown = ckbIsDown.Checked ? "1" : "0";
            string actiontype = Request.QueryString["type"];
            if (actiontype == "insert")
            {
                model.Classid = Request.QueryString["classid"];
                service.Insert(model);
            }
            else if (actiontype == "edit")
            {
                model.Classid = hid_classid.Value;
                model.Pid = Convert.ToInt32(Request.QueryString["pid"]);
                service.Update(model);
            }

            string str_alert = "<script type=\"text/javascript\" language=\"javascript\">          alert('保存成功')　　; </script>";
            string str_url = "<script type=\"text/javascript\" language=\"javascript\">          window.parent.frames[\"productDtlIframe\"].location=\"/systemanage/product/productlist.aspx?classid=" + model.Classid + "\"　　; </script>";
            Response.Write(str_alert);
            Response.Write(str_url); 
        }
    }
}
